package Classes.Managers// ActionScript file
{
	import Classes.Interfaces.I_TimeUnit;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 *时序管理器 
	 * @author 葛增文
	 * 
	 */	
	public class M_Timer extends Manager
	{
		private static var _instance:M_Timer;/*静态引用*/
		private var _nowTime:Number;/*当前时间*/
		private var _timeUnitAry:Array;/*记时对象*/
		public function M_Timer():void
		{
			_instance=this;
			this.init();
		}
		
		private function init():void
		{
			this._timeUnitAry=new Array;
		}
		
		/**
		 *设定当前时间并启动计时器 
		 * @param time 当前时间
		 * 
		 */		
		public function setNowTime(time:Number):void
		{
			this.nowTime=time;
			var timer:Timer=new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER,timeUpdateHandle);
			timer.start();
		}
		
		/**
		 *时间更新操作 
		 * @param e
		 * 
		 */		
		private function timeUpdateHandle(e:TimerEvent):void
		{
			this.nowTime+=1;
			for(var i:int=0;i<this.timeUnitAry.length;i++)
			{
				var timeUnit:I_TimeUnit=this.timeUnitAry[i];
				if(!timeUnit.isUpdateTime)
				{
					this.timeUnitAry.splice(i,1);
					i--;
				}else{
					timeUnit.timeUpdate();
				}
			}
		}
		
		/**
		 *加入时序对象 
		 * @param timeUnit 时序对象
		 * 
		 */		
		public function joinUnit(timeUnit:I_TimeUnit):void
		{
			this.timeUnitAry.push(timeUnit);
			timeUnit.isUpdateTime=true;
		}
		
		/**
		 *释放 
		 * 
		 */		
		public function release():void
		{
			this._timeUnitAry=null;
			_instance=null;
		}
		
		/*getter & setter*/
		public static function get instance():M_Timer
		{
			return _instance;
		}

		public function get nowTime():Number
		{
			return _nowTime;
		}

		public function get timeUnitAry():Array
		{
			return _timeUnitAry;
		}

		public function set nowTime(value:Number):void
		{
			_nowTime = value;
		}


	}
}